# -*- coding: utf-8 -*-

from .CharConvert import CharConvert


class SelectionConvert(CharConvert):
    field_type = 'selection'

    @classmethod
    def to_read(cls, rec, field, **kwargs):
        if callable(field.selection):
            field_selection = field.selection(rec)
        elif isinstance(field.selection, str):
            field_selection = getattr(rec, field.selection)()
        else:
            field_selection = field.selection

        return dict(field_selection).get(rec[field.name])

    @classmethod
    def to_domain(cls, field, value, **kwargs):
        return [(field.name, '=', value)]

